package com.yhyvr.api.service.store;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoWebNewsMapper;
import com.lz.mybatis.jdbc.auto.model.BoWebNews;
import com.lz.mybatis.jdbc.auto.model.BoWebNewsExample;
import com.lz.tool.date.DateStyle;
import com.lz.tool.date.DateUtil;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.peoblem.ApiNewsDetailReq;
import com.yhyvr.pojo.req.api.store.peoblem.ApiNewsListReq;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.store.problem.ApiNewsDetailResp;
import com.yhyvr.pojo.resp.api.store.problem.ApiNewsListResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhangqin on 2016/11/29.
 *新闻或者问题接口
 */
@Service
public class ApiNewsService {

    @Resource
    private BoWebNewsMapper boWebNewsMapper;
    public ApiRespData<List<ApiNewsListResp>> newsList(ApiReqData<ApiNewsListReq> apiNewsListReqApiReqData){
        ApiNewsListReq apiNewsListReq = apiNewsListReqApiReqData.getBody();
        BoWebNewsExample boWebNewsExample = new BoWebNewsExample();
        boWebNewsExample.createCriteria().andPublishEqualTo(1).andNewsTypeEqualTo(apiNewsListReq.getType());
        PageHelper.startPage(apiNewsListReq.getPageNumber(),apiNewsListReq.getPageSize());
        List<BoWebNews> list =  boWebNewsMapper.selectByExample(boWebNewsExample);
        List<ApiNewsListResp> listApiNewsListResp = Lists.newArrayList();
        if(list!=null&&list.size()>0){
            for (BoWebNews boWebNews : list) {
                ApiNewsListResp apiNewsListResp = new ApiNewsListResp();
                apiNewsListResp.setCreateTime(DateUtil.DateToString(boWebNews.getCreateTime(), DateStyle.YYYY_MM_DD_CN));
                apiNewsListResp.setId(boWebNews.getId());
                apiNewsListResp.setTitle(boWebNews.getTitle());
                listApiNewsListResp.add(apiNewsListResp);
            }
        }
        return ApiRespData.buildSucc(listApiNewsListResp);
    }

    public ApiRespData<ApiNewsDetailResp> newsDetail(ApiReqData<ApiNewsDetailReq> apiNewsDetailReq){
        ApiNewsDetailReq apiNewsDetail = apiNewsDetailReq.getBody();
        BoWebNews boWebNews = boWebNewsMapper.selectByPrimaryKey(apiNewsDetail.getId());
        if(boWebNews!=null){
                ApiNewsDetailResp apiNewsListResp = new ApiNewsDetailResp();
                apiNewsListResp.setCreateTime(DateUtil.DateToString(boWebNews.getCreateTime(), DateStyle.YYYY_MM_DD_CN));
                apiNewsListResp.setId(boWebNews.getId());
                apiNewsListResp.setTitle(boWebNews.getTitle());
                apiNewsListResp.setContent(boWebNews.getContent());
            return ApiRespData.buildSucc(apiNewsListResp);
        }else{
            return ApiRespData.buildFail();
        }

    }
}
